package com.gdut.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdut.eduservice.entity.EduCourse;
import com.gdut.eduservice.entity.EduCourseDescription;
import com.gdut.eduservice.entity.frontvo.CourseFrontVo;
import com.gdut.eduservice.entity.frontvo.CourseWebVo;
import com.gdut.eduservice.entity.vo.CourseInfoVo;
import com.gdut.eduservice.entity.vo.CoursePublishVo;
import com.gdut.eduservice.mapper.EduCourseMapper;
import com.gdut.eduservice.service.EduChapterService;
import com.gdut.eduservice.service.EduCourseDescriptionService;
import com.gdut.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdut.eduservice.service.EduVideoService;
import com.gdut.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-23
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;


    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        int insert = baseMapper.insert(eduCourse);

        if (insert == 0){
            throw new GuliException(20001,"添加课程信息失败");
        }

        String cid = eduCourse.getId();

        EduCourseDescription courseDescription = new EduCourseDescription();

        courseDescription.setDescription(courseInfoVo.getDescription());

        courseDescription.setId(cid);

        courseDescriptionService.save(courseDescription);


        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        //1查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());


        return courseInfoVo;
    }


//    修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {

        //1修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);

        if (update == 0){
            throw new GuliException(20001,"修改课程信息失败");
        }

        //2修改描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescriptionService.updateById(courseDescription);
        



    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublishVo(id);
        return coursePublishVo;
    }

    //删除课程
    @Override
    public void removeCourse(String courseId) {

        //1根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);

        //2根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);


        //3根据课程id删除描述
        courseDescriptionService.removeById(courseId);

        
        //4根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);

        if (result == 0){
            throw new GuliException(20001,"删除失败");
        }


    }

    //查询前8条热门课程，
    @Cacheable(key = "'selectCourseList'",value = "course")
    @Override
    public List<EduCourse> selectCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<EduCourse> eduList = baseMapper.selectList(wrapper);
        return eduList;
    }

    //1条件查询带分页查询课程
    @Override
    public HashMap<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {

        //2根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //判断条件值是否为空，不为空拼接
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, wrapper);
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        List<EduCourse> records = pageParam.getRecords();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasPrevious = pageParam.hasPrevious();
        boolean hasNext = pageParam.hasNext();

        HashMap<String, Object> map = new HashMap<>();
        map.put("current",current);
        map.put("pages",pages);
        map.put("total",total);
        map.put("records",records);
        map.put("size",size);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);


        return map;
    }

    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {


        

        return baseMapper.getBaseCourseInfo(courseId);
    }
}
